<?php

namespace app\admin\controller;
use app\admin\model\AdminLogs;
use app\admin\model\AdminUsers;

class AdminLog extends Base
{
    /**
     * 日志列表
     * @return \think\Response
     */
    public function index()
    {
        $model      = new AdminLogs();
        $page_param = ['query' => []];
        if (isset($this->param['user_id']) && ($this->param['user_id']) > 0) {
            $user_id                        = $this->param['user_id'];
            $page_param['query']['user_id'] = $user_id;
            $model->where('user_id', $user_id);
            $this->assign('user_id', $user_id);
        }
        if (isset($this->param['start_date']) && !empty($this->param['start_date'])) {
            $page_param['query']['start_date'] = $this->param['start_date'];
            $start_date                        = $this->param['start_date'];
            $model->whereTime('create_time', '>=', $start_date);
            $this->assign('start_date', $this->param['start_date']);
        }

        if (isset($this->param['end_date']) && !empty($this->param['end_date'])) {
            $page_param['query']['end_date'] = $this->param['end_date'];
            $end_date                        = $this->param['end_date'];
            $model->whereTime('create_time', '<=', strtotime($end_date . '+1 day'));
            $this->assign('end_date', $this->param['end_date']);
        }
        if (isset($this->param['export_data']) && $this->param['export_data'] == 1) {
            $data   = $model->with('adminUser')->select();
            foreach ($data  as $k =>&$v){
              $v['username']=$v->adminuser->name;
            }

            $fileName ='统计数据';
            //设置表头
            $xlsCell  = array(
                array('id',"ID",15),
                array('username','用户名',15),
                array('title','操作',15),
                array('log_url','URL',20),
                array('log_type','请求方式',20),
                array('log_ip','ip地址',40),
                array('create_time','日期',25),
            );
            $xlsName  = "Excel";
            exportExcel($xlsName,$xlsCell,$data,$fileName);//excel 导出
            ob_clean();//清除缓存
        }
        $list = $model->field('id,user_id,title,log_url,log_type,log_ip,create_time')
            ->with('adminUser')
            ->order('id desc')
            ->paginate($this->webData['list_rows'], false, $page_param);

        $this->assign([
            'list'      => $list,
            'page'      => $list->render(),
            'total'     => $list->total(),
            'user_list' => AdminUsers::all()
        ]);
        return view();
    }



    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }


    public function export()
    {

    }

}
